import module namespace common = "http://www.metaphoricalweb.com/xmlns/common" at "/xrai/common/xq/common.xq";
declare option exist:serialize "media-type=application/atom+xml";
declare namespace atom="http://www.w3.org/2005/Atom";

let $format := common:get-parameter("format","xml")
let $id := common:get-parameter("id","")
let $object := common:get-by-id($id)
let $type := substring-before(name($object/*),":")
let $result :=  
if ($format = "xml") then (util:declare-option("exist:serialize","media-type=text/xml"),$object) else
if ($format = "atom") then 
(util:declare-option("exist:serialize","media-type=application/atom+xml"),
<atom:feed>
     <atom:title>Results of getbyid</atom:title>
     <atom:author>CASRAI.org</atom:author>
     <atom:id>{util:uuid()}</atom:id>
     <atom:issued>{current-dateTime()}</atom:issued>
     <atom:modified>{current-dateTime()}</atom:modified>
     <atom:created>{current-dateTime()}</atom:created>
     <atom:link href="To Be Determined"/>
     <atom:category schema="xrai" term="{$type}"/>     
    <atom:entry>
         <atom:title>{string($object//*:Metadata/*:Title)}</atom:title>
         <atom:author>CASRAI.org</atom:author>
         <atom:id>{util:uuid()}</atom:id>
         <atom:issued>{string($object//*:Metadata/*:DateCreated)}</atom:issued>
         <atom:modified>{string($object//*:Metadata/*:DateLastModified)}</atom:modified>
         <atom:created>{string($object//*:Metadata/*:DateCreated)}</atom:created>
         <atom:link href="http://localhost:8080/exist/rest//db/xrai/common/xq/editor.xq?id={$id};type=cv"/>
         <atom:category schema="xrai" term="{$type}"/>     
        <atom:content type="application/xml">{$object}</atom:content>
    </atom:entry>
</atom:feed>)
else (util:declare-option("exist:serialize","media-type=text/xml"),$object)
return $result